package com.sgjq.forum.services.impl;

import com.sgjq.forum.common.AppResult;
import com.sgjq.forum.common.ResultCode;
import com.sgjq.forum.dao.ArticleReplyMapper;
import com.sgjq.forum.exception.ApplicationException;
import com.sgjq.forum.model.Article;
import com.sgjq.forum.model.ArticleReply;
import com.sgjq.forum.services.IArticleReplyService;
import com.sgjq.forum.services.IArticleService;
import com.sgjq.forum.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleReplyServiceImpl implements IArticleReplyService {

    @Resource
    private ArticleReplyMapper articleReplyMapper;
    @Resource
    private IArticleService articleService;

    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        // 非空校验
        if (articleId == null || articleId <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 调用DAO
        List<ArticleReply> result = articleReplyMapper.selectByArticleId(articleId);
        // 返回结果
        return result;
    }

    @Override
    public void create(ArticleReply articleReply) {
        // 非空校验
        if (articleReply == null || StringUtils.isEmpty(articleReply.getContent())
                || articleReply.getArticleId() == null || articleReply.getArticleId() <= 0
                || articleReply.getPostUserId() == null || articleReply.getPostUserId() <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 查询帖子对象
        Article article = articleService.selectById(articleReply.getArticleId());
        // 校验帖子有效性
        if (article == null || article.getDeleteState() == 1) {
            // 打印日志
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        // 校验帖子状态
        if (article.getState() == 1) {
            // 打印日志
            log.warn(ResultCode.FAILED_ARTICLE_STATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_STATE));
        }
        // 构建默认值
        articleReply.setLikeCount(0); // 点赞数
        articleReply.setState((byte) 0); // 状态
        articleReply.setDeleteState((byte) 0); // 是否删除
        Date date = new Date();
        articleReply.setCreateTime(date); // 创建时间
        articleReply.setUpdateTime(date); // 更新时间

        // 调用DAO，写入数据库
        int articleReplyRow = articleReplyMapper.insertSelective(articleReply);
        if (articleReplyRow != 1) {
            // 打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }

        // 更新帖子的回复数量
        Article updateArticle = new Article();
        updateArticle.setId(article.getId()); // 帖子Id
        updateArticle.setReplyCount(article.getReplyCount() + 1); // 设置回复数量
        updateArticle.setUpdateTime(new Date()); // 更新时间

        // 调用Article的Service完成更新操作
        articleService.updateById(updateArticle);

        // 打印日志
        log.info(ResultCode.SUCCESS.toString() + " 回复发布成功, articleId = " + article.getId());
    }
}
